package com.mf.morefun.ui.moreread;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.mf.morefun.R;
import com.mf.morefun.adapter.ListenAdapter;
import com.mf.morefun.adapter.ReadDetailAdapter;
import com.mf.morefun.adapter.ReadDetailTypeAdapter;
import com.mf.morefun.base.BaseBusActivity;
import com.mf.morefun.bean.EventMessageBean;
import com.mf.morefun.bean.ListenBean;
import com.mf.morefun.bean.ReadDetailBean;
import com.mf.morefun.bean.ReadDetailTypeBean;
import com.mf.morefun.bean.ResultBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.listener.ReadDetailTypeListener;
import com.mf.morefun.requestbean.RequestAddEars;
import com.mf.morefun.requestbean.RequestListenBean;
import com.mf.morefun.requestbean.RequestReadDetailBean;
import com.mf.morefun.requestbean.RequestReadDetailTypeBean;
import com.mf.morefun.requestbean.RequestSaveBookMarkBean;
import com.mf.morefun.ui.VideoActivity;
import com.mf.morefun.ui.dohomework.HomeworkListActivity;
import com.mf.morefun.ui.grindingears.GrindingEarsActivity;
import com.mf.morefun.ui.myclass.CreateClassHomeWorkActivity;
import com.mf.morefun.ui.userinfo.LoginActivity;
import com.mf.morefun.util.GetUserInfo;
import com.mf.morefun.util.HaveTestUtil;
import com.mf.morefun.util.IsLoginBean;
import com.mf.morefun.widget.NetErrorPop;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoreReadDetailActivity extends BaseBusActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_allselect)
    TextView tvAllselect;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.gv_info)
    GridView gvInfo;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private String mReadId = "";
    private String mReadName = "";
    private List<ReadDetailTypeBean> mReadDetailTypeList = new ArrayList<>();
    private ReadDetailTypeAdapter readDetailTypeAdapter;
    private List<ReadDetailBean> mReadDetailList = new ArrayList<>();
    private ReadDetailAdapter mReadDetailAdapter;
    private String uiType = "0";
    private SPUtils mSputils;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_morereaddetail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSputils = new SPUtils(getApplicationContext());
        if (!IsLoginBean.isNo(false)){
            GetUserInfo.initLogin(mSputils.getString(Configuration.USERPHONE), mSputils.getString(Configuration.USERPASSWORD));
        }
        mReadId = getIntent().getStringExtra("READID");//找到系列ID
        mReadName = getIntent().getStringExtra("READNAME");//名字
        LinearLayoutManager ms = new LinearLayoutManager(this);
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvType.setLayoutManager(ms);
        tvTitle.setText(mReadName);
        uiType = getIntent().getStringExtra("type");//0代表首页过来的  1代表磨耳朵过来的
        if (!uiType.equals("1")){
            llRight.setVisibility(View.GONE);
        }else {
            llRight.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        ReadDetailTypeBean readDetailTypeBean = new ReadDetailTypeBean();
        readDetailTypeBean.setName("单元");
        mReadDetailTypeList.add(readDetailTypeBean);
        readDetailTypeAdapter = new ReadDetailTypeAdapter(MoreReadDetailActivity.this, mReadDetailTypeList);
        readDetailTypeAdapter.setSelectIndex(0);
        rvType.setAdapter(readDetailTypeAdapter);
        readDetailTypeAdapter.setListener(new ReadDetailTypeListener() {
            @Override
            public void callBack(int position) {
                if (StringUtils.isEmpty(mReadDetailTypeList.get(position).getId())) {
                    return;
                }
                readDetailTypeAdapter.setSelectIndex(position);
                readDetailTypeAdapter.notifyDataSetChanged();
                //点击
                initGetReadDetail(mReadDetailTypeList.get(position).getId());
            }
        });
        //先根据系列ID找到单元列表
        initGetReadDetailType();
        mReadDetailAdapter = new ReadDetailAdapter(MoreReadDetailActivity.this, mReadDetailList);
        gvInfo.setAdapter(mReadDetailAdapter);
        gvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != mReadDetailList && mReadDetailList.size() > 0){
                    if (uiType.equals("0")){
                        if (null != mReadDetailList.get(position).getMfVideoList() && mReadDetailList.get(position).getMfVideoList().size() > 0){
                            //看视频
                            Intent mIntent = new Intent(MoreReadDetailActivity.this, VideoActivity.class);
                            mIntent.putExtra("VIDEO", mReadDetailList.get(position).getMfVideoList().get(0).getAddress());
                            mIntent.putExtra("BOOKID", mReadDetailList.get(position).getId());
                            mIntent.putExtra(Configuration.USERID, mSputils.getString(Configuration.USERID));
                            startActivity(mIntent);
                        }else {
                            //弹出一个dialog
                            showDetailDialog(position);
                        }
                    }else if(uiType.equals("1")){
                        //选择
                        if (mReadDetailList.get(position).isSelected()){
                            mReadDetailList.get(position).setSelected(false);
                        }else {
                            mReadDetailList.get(position).setSelected(true);
                        }
                        mReadDetailAdapter.notifyDataSetChanged();
                    }else {
                        if (uiType.equals("3")){
                            if (null != mReadDetailList.get(position).getMfVideoList() && mReadDetailList.get(position).getMfVideoList().size() > 0){
                                Intent intent = new Intent();
                                intent.setAction("HOMEWORKDATA");
                                intent.putExtra("BOOKNAME", mReadDetailList.get(position).getName());
                                intent.putExtra("BOOKID", mReadDetailList.get(position).getId());
                                intent.putExtra("LISTENID", "");
                                intent.putExtra("type", uiType);
                                intent.putExtra("BOOKIMAGE", mReadDetailList.get(position).getCover());
                                sendBroadcast(intent);
                                Intent mIntent = new Intent(MoreReadDetailActivity.this, CreateClassHomeWorkActivity.class);
                                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                startActivity(mIntent);
                                finish();
                            }else {
                                ToastUtils.show(getApplicationContext(), "类型错误", 100);
                            }
                        }else {
                            initGetListen(mReadDetailList.get(position).getId(), mReadDetailList.get(position).getName(), 1, mReadDetailList.get(position).getCover());
                        }
                    }
                }
            }
        });
    }

    private void initSaveWork(String bookId){
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.BOOKMARKSAVE);
        RequestSaveBookMarkBean requestSaveBookMarkBean = new RequestSaveBookMarkBean();
        RequestSaveBookMarkBean.BookBean bookBean = new RequestSaveBookMarkBean.BookBean();
        bookBean.setId(bookId);
        requestSaveBookMarkBean.setBook(bookBean);
        RequestSaveBookMarkBean.UserBean userBean = new RequestSaveBookMarkBean.UserBean();
        userBean.setId(mSputils.getString(Configuration.USERID));
        requestSaveBookMarkBean.setUser(userBean);
        params.setBodyContent(JSON.toJSONString(requestSaveBookMarkBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)){
                    ResultBean mResult = JSON.parseObject(result, ResultBean.class);
                    if (mResult.getErrorCode() != 0){
                        ToastUtils.show(getApplicationContext(), mResult.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //点击弹出可以
    private void showDetailDialog(int position) {
        final Dialog dialog = new Dialog(MoreReadDetailActivity.this, R.style.loginerrordialog);
        View view = LayoutInflater.from(MoreReadDetailActivity.this).inflate(R.layout.layout_dialogdetail, null);
        dialog.setContentView(view);
        LinearLayout ll_explain = view.findViewById(R.id.ll_explain);
        LinearLayout ll_fundamental = view.findViewById(R.id.ll_fundamental);
        LinearLayout ll_recording = view.findViewById(R.id.ll_recording);
        LinearLayout ll_test = view.findViewById(R.id.ll_test);
        if (mReadDetailList.get(position).getHaveListenFlag().equals("1")) {
            ll_explain.setBackgroundResource(R.drawable.blue_shape);
        } else {
            ll_explain.setBackgroundResource(R.drawable.gray_shape);
        }
        if (mReadDetailList.get(position).getHaveTestFlag().equals("1")) {
            ll_test.setBackgroundResource(R.drawable.blue_shape);
        } else {
            ll_test.setBackgroundResource(R.drawable.gray_shape);
        }
        initListener(position, ll_explain, ll_fundamental, ll_recording, ll_test, dialog);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DeviceUtils.deviceWidth(mContext) * 0.8);
        lp.height = (int) (DeviceUtils.deviceWidth(mContext) * 0.22);
        dialogWindow.setAttributes(lp);
    }

    private void initListener(int position, LinearLayout ll_explain, LinearLayout ll_fundamental, LinearLayout ll_recording, LinearLayout ll_test, Dialog dialog) {
        ll_explain.setOnClickListener(new View.OnClickListener() {//听讲解
            @Override
            public void onClick(View v) {
                if (mReadDetailList.get(position).getHaveListenFlag().equals("1")){
                    dialog.dismiss();
                    initSaveWork(mReadDetailList.get(position).getId());
                    initGetListen(mReadDetailList.get(position).getId(), mReadDetailList.get(position).getName(), 0, "");
                }else {
                    ToastUtils.show(getApplicationContext(), "当前还没有老师讲解呢！", 100);
                }
            }
        });
        ll_fundamental.setOnClickListener(new View.OnClickListener() {//听原音
            @Override
            public void onClick(View v) {
                initSaveWork(mReadDetailList.get(position).getId());
                Intent mIntent = new Intent(MoreReadDetailActivity.this, ListenOriginalActivity.class);
                mIntent.putExtra("BOOKID", mReadDetailList.get(position).getId());
                mIntent.putExtra("BOOKNAME", mReadDetailList.get(position).getName());
                mIntent.putExtra(Configuration.USERID, mSputils.getString(Configuration.USERID));
                startActivity(mIntent);
            }
        });
        ll_recording.setOnClickListener(new View.OnClickListener() {//录音
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(MoreReadDetailActivity.this, ListenRecordActivity.class);
                mIntent.putExtra("BOOKID", mReadDetailList.get(position).getId());
                mIntent.putExtra("TYPE", "1");
                mIntent.putExtra("BOOKNAME", mReadDetailList.get(position).getName());
                mIntent.putExtra("CONTEXTTYPE", "1");
                mIntent.putExtra("MEDIATYPE", "1");
                mIntent.putExtra("CONTEXTID", mReadDetailList.get(position).getId());
                mIntent.putExtra(Configuration.USERID, mSputils.getString(Configuration.USERID));
                startActivity(mIntent);
            }
        });
        ll_test.setOnClickListener(new View.OnClickListener() {//测试
            @Override
            public void onClick(View v) {
                if (mReadDetailList.get(position).getHaveTestFlag().equals("1")) {
                    HaveTestUtil mHave = new HaveTestUtil();
                    mHave.initGetTest(MoreReadDetailActivity.this, mReadDetailList.get(position).getId(), mSputils.getString(Configuration.USERID));
                }else {
                    ToastUtils.show(getApplicationContext(), "当前绘本还没有测试题！", 100);
                }
            }
        });
    }

    private void initGetListen(String bookId, String bookName, int type, String bookImg) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.LISTENTEACHER);
        RequestListenBean requestListenBean = new RequestListenBean();
        RequestListenBean.BookBean bookBean = new RequestListenBean.BookBean();
        bookBean.setId(bookId);
        requestListenBean.setBook(bookBean);
        params.setBodyContent(JSON.toJSONString(requestListenBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            List<ListenBean> listenBeanList = JSON.parseArray(resultBean.getResult(), ListenBean.class);
                            if (null != listenBeanList && listenBeanList.size() > 0){
                                try {
                                    showExplainPop(listenBeanList, bookId, bookName, type, bookImg);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            Intent intent = new Intent();
                            intent.setAction("HOMEWORKDATA");
                            intent.putExtra("BOOKNAME", bookName);
                            intent.putExtra("BOOKID", bookId);
                            intent.putExtra("LISTENID", "");
                            intent.putExtra("type", uiType);
                            intent.putExtra("BOOKIMAGE", bookImg);
                            sendBroadcast(intent);
                            Intent mIntent = new Intent(MoreReadDetailActivity.this, CreateClassHomeWorkActivity.class);
                            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                            startActivity(mIntent);
                            finish();
                        }
                    } else {
                        ToastUtils.show(getApplicationContext(), resultBean.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private PopupWindow mExplainPop = null;
    private void showExplainPop(List<ListenBean> listenBeanList, String bookId, String bookName, int type, String bookImg) {
        closePop();
        if (mExplainPop == null) {
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.layout_popexplain, null);
            // 这里启动头像
            RelativeLayout rl_bg = (RelativeLayout) view.findViewById(R.id.rl_bg);
            rl_bg.getBackground().setAlpha(100);
            ListView lv_explain = view.findViewById(R.id.lv_explain);
            ListenAdapter adapter = new ListenAdapter(MoreReadDetailActivity.this, listenBeanList);
            lv_explain.setAdapter(adapter);
            lv_explain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    closePop();
                    if (type == 0){
                        //开始听讲解
                        Intent mIntent = new Intent(MoreReadDetailActivity.this, ListenTeacherActivity.class);
                        mIntent.putExtra("BOOKNAME", listenBeanList.get(position).getName());
                        mIntent.putExtra("LISTENBEAN", listenBeanList.get(position));
                        mIntent.putExtra(Configuration.USERID, mSputils.getString(Configuration.USERID));
                        startActivity(mIntent);
                    }else {
                        Intent intent = new Intent();
                        intent.setAction("HOMEWORKDATA");
                        intent.putExtra("BOOKNAME", listenBeanList.get(position).getName());
                        intent.putExtra("BOOKID", bookId);
                        intent.putExtra("LISTENID", listenBeanList.get(position).getId());
                        intent.putExtra("type", uiType);
                        intent.putExtra("BOOKIMAGE", bookImg);
                        sendBroadcast(intent);
                        Intent mIntent = new Intent(MoreReadDetailActivity.this, CreateClassHomeWorkActivity.class);
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        startActivity(mIntent);
                        finish();
                    }
                }
            });
            TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
            tv_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    closePop();
                }
            });
            int h = getWindowManager().getDefaultDisplay().getHeight();
            mExplainPop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, h);
            mExplainPop.setFocusable(true);
            mExplainPop.setOutsideTouchable(true);
            mExplainPop.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // 这里如果返回true的话，touch事件将被拦截
                    // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                    return false;
                }
            });
            mExplainPop.setBackgroundDrawable(new BitmapDrawable());
            Rect rect = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int winHeight = getWindow().getDecorView().getHeight();
            mExplainPop.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
        }
    }

    private void closePop(){
        if (null != mExplainPop){
            mExplainPop.dismiss();
            mExplainPop = null;
        }
    }
    
    private void initGetReadDetail(String id) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.READDETAIL);
        RequestReadDetailBean requestReadDetailBean = new RequestReadDetailBean();
        RequestReadDetailBean.UnitBean unitBean = new RequestReadDetailBean.UnitBean();
        unitBean.setId(id);
        requestReadDetailBean.setUnit(unitBean);
        params.setBodyContent(JSON.toJSONString(requestReadDetailBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mReadDetailList.clear();
                if (!StringUtils.isEmpty(result)) {
                    ResultBean mResultBean = JSON.parseObject(result, ResultBean.class);
                    if (mResultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(mResultBean.getResult())) {
                            List<ReadDetailBean> readDetailBeanList = JSON.parseArray(mResultBean.getResult(), ReadDetailBean.class);
                            mReadDetailList.addAll(readDetailBeanList);
                            //吧所有的标志设为false
                            setFalse();
                        } else {
                            ToastUtils.show(getApplicationContext(), "数据为空", 100);
                        }
                    } else {
                        ToastUtils.show(getApplicationContext(), mResultBean.getMessage(), 100);
                    }
                }
                mReadDetailAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void setFalse() {
        if (null != mReadDetailList && mReadDetailList.size() > 0){
            for (int i = 0; i < mReadDetailList.size(); i++){
                mReadDetailList.get(i).setSelected(false);
            }
        }
    }

    private void setTrue() {
        if (null != mReadDetailList && mReadDetailList.size() > 0){
            for (int i = 0; i < mReadDetailList.size(); i++){
                mReadDetailList.get(i).setSelected(true);
            }
        }
    }

    private void initGetReadDetailType() {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.READDETAILTYPE);
        RequestReadDetailTypeBean.SeriesBean seriesBean = new RequestReadDetailTypeBean.SeriesBean();
        seriesBean.setId(mReadId);
        RequestReadDetailTypeBean requestReadDetailTypeBean = new RequestReadDetailTypeBean();
        requestReadDetailTypeBean.setSeries(seriesBean);
        params.setBodyContent(JSON.toJSONString(requestReadDetailTypeBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mReadDetailTypeList.clear();
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            List<ReadDetailTypeBean> readDetailTypeBeans = JSON.parseArray(resultBean.getResult(), ReadDetailTypeBean.class);
                            mReadDetailTypeList.addAll(readDetailTypeBeans);
                            readDetailTypeAdapter.setSelectIndex(0);
                            initGetReadDetail(mReadDetailTypeList.get(0).getId());
                        } else {
                            ToastUtils.show(getApplicationContext(), "当前系列还没有数据呢！", 100);
                        }
                    } else {
                        ToastUtils.show(getApplicationContext(), "当前系列还没有数据呢！", 100);
                    }
                }
                if (mReadDetailTypeList.size() == 0) {
                    ReadDetailTypeBean readDetailTypeBean = new ReadDetailTypeBean();
                    readDetailTypeBean.setName("单元");
                    mReadDetailTypeList.add(readDetailTypeBean);
                    readDetailTypeAdapter.setSelectIndex(0);
                }
                readDetailTypeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_allselect, R.id.tv_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_allselect:
                setTrue();
                mReadDetailAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_finish:
                //把选择的东西传过去
                //先判断是否添加了一条
                //得到选择的集合
                List<ReadDetailBean> mReadList = new ArrayList<>();
                getSelectedList(mReadList);
                if (mReadList.size() > 0){
                    initAddEars(mReadList);
                }else {
                    ToastUtils.show(getApplicationContext(), "请选择...", 100);
                }
                break;
        }
    }

    private void initAddEars(List<ReadDetailBean> mReadList) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.ADDEARS);
        RequestAddEars requestAddEars = new RequestAddEars();
        List<RequestAddEars.ListBean> list = new ArrayList<>();
        for (int i = 0; i < mReadList.size(); i ++){
            RequestAddEars.ListBean listBean = new RequestAddEars.ListBean();
            RequestAddEars.ListBean.UserBean userBean = new RequestAddEars.ListBean.UserBean();
            userBean.setId(mSputils.getString(Configuration.USERID));
            listBean.setUser(userBean);
            RequestAddEars.ListBean.BookBean bookBean = new RequestAddEars.ListBean.BookBean();
            bookBean.setId(mReadList.get(i).getId());
            listBean.setBook(bookBean);
            list.add(listBean);
        }
        requestAddEars.setList(list);
        params.setBodyContent(JSON.toJSONString(requestAddEars));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)){
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0){
                        Intent mIntent = new Intent(MoreReadDetailActivity.this, GrindingEarsActivity.class);
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        startActivity(mIntent);
                        finish();
                        EventMessageBean messageBean = new EventMessageBean();
                        messageBean.setType(1099);
                        EventBus.getDefault().post(messageBean);
                    }else {
                        ToastUtils.show(getApplicationContext(), resultBean.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(MoreReadDetailActivity.this);
                    errorPop.showPop("");
                    break;
            }
        }
    };

    private void getSelectedList(List<ReadDetailBean> mReadList) {
        if (null != mReadDetailList && mReadDetailList.size() > 0){
            for (int i = 0; i < mReadDetailList.size(); i++){
                if (mReadDetailList.get(i).isSelected()){
                    mReadList.add(mReadDetailList.get(i));
                }
            }
        }
    }
}
